#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include "choose.h"
#include <QPushButton>
#include <QMediaPlayer>
#include <QAudioOutput>             //要包含两个音乐播放的头文件

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget),
     chooseWidget(nullptr)
{


    ui->setupUi(this);
    player = new QMediaPlayer(this);                  //new一下
    audioOutput = new QAudioOutput(this);//new一下
    player->setAudioOutput(audioOutput);             //音乐输出

    player->setSource(QUrl("qrc:/a/reflexion_allegretto_you.mp3"));    //sourcefile  注意路径

     player->setLoops(QMediaPlayer::Infinite);               //循环播放

     audioOutput->setVolume(30);                          //音量

     player->play();                               //开始播放


    this->resize(1000, 600);      //固定窗口大小
    QPushButton *button = ui->pushButton; // 确保按钮objectName是pushButton
    connect(button, &QPushButton::clicked, this, &Widget::on_pushButton_clicked);           //信号与槽

    //美化按钮
    ui->pushButton->setFixedSize(120, 60);

    ui->pushButton->setStyleSheet(
        "QPushButton {"
        "    border-radius: 10px;"          // 圆角半径
        "    background-color:#FFD1DC ;"    // 背景色
        "    color: white;"                // 文字颜色
        "}"
        "QPushButton:hover {"
        "    background-color: #FFD1DC;"   // 悬停状态颜色
        "}"
        "QPushButton:pressed {"
        "    background-color: #FFD1DC;"   // 按下状态颜色
        "}"
        );






}



Widget::~Widget()
{
    delete player;
    delete audioOutput;                     //释放
    delete ui;
    if(chooseWidget) delete chooseWidget;
}

void Widget::on_pushButton_clicked()
{
    if(!chooseWidget) {
        chooseWidget = new Choose();  // 确保Choose类已正确定义
    }

    this->hide();
    chooseWidget->show();
    qDebug() << "Jump to Choose window";         // 跳转

    player->pause();
    qDebug() << "Audio paused";                    //跳转至下个窗口时音乐暂停
}

void Widget::paintEvent(QPaintEvent * event)
{
    Q_UNUSED(event);
    QPainter painter(this);
    painter.drawPixmap(0,0,width(),height(),QPixmap(":/1/bg1.bmp"));      //画背景
}
